<?php

class Admin_RolesController extends Zend_Controller_Action
{

    function indexAction()
    {
        $form = $this->_getRolesForm();
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $roles = new Default_Model_AclRoles();
                $res = $roles->getTable()->findByid($form->getValue('id'))->toArray();
                if (!$res) {
                    $roles->id =$form->getValue('id');
                    $roles->role_kind = Default_Model_AclRoles::ROLE_CUSTOM;
                    $roles->save();
                    $form->getElement('id')->setValue('');
;                }
                else
                    $form->addErrorMessage('double role');
            }
        }
        $this->view->form = $form;
        $aclRoleCustom = Doctrine::getTable('Default_Model_AclRoles')
        ->findByDql('role_kind=?', Default_Model_AclRoles::ROLE_CUSTOM);
        $this->view->data = $aclRoleCustom;
    }

    protected function _getRolesForm()
    {
        $form = new Zend_Form() ;

        $form->addElement('text', 'id', array(
            'label' => 'role name',
            'size' => 16,
            'required' => true,
            'filters'    => array('StringTrim','StripTags'),
            'validators' => array('NotEmpty')
        ));
        $form->addElement('submit', 'submit',array(
            'label' => 'append role'
            ));

        return $form;
    }
}